

Imports System.Collections
Imports System.IO
Imports System.Linq
Imports System.Net
Imports System.ServiceModel
Imports System.ServiceModel.Syndication
Imports System.ServiceModel.Web
Imports System.Xml
Imports System.Xml.Schema

Imports Microsoft.VisualStudio.TestTools.UnitTesting

<TestClass()> _
Public Class PictureServiceFixture
   

    Private testContextInstance As TestContext

    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = value
        End Set
    End Property

    <ClassInitialize()> _
    Public Shared Sub FixtureInitialize(ByVal testContext As TestContext)
        PictureServiceHostHelper.Start()
    End Sub

    <ClassCleanup()> _
    Public Shared Sub FixtureCleanup()
        PictureServiceHostHelper.Stop()
    End Sub

    <TestMethod()> _
    Public Sub TestGetPicturesAsRssItemCount()
        Dim reader As XmlReader = XmlReader.Create(BuildUri(ServiceUris.GetPicturesAsRss).ToString)

        Dim feed As SyndicationFeed = SyndicationFeed.Load(reader)
        Dim expected As Integer = PictureServiceSettings.DefaultFeedItemsCount
        Dim actual As Integer = feed.Items.Count()
        Assert.AreEqual(Of Integer)(expected, actual)
    End Sub

    <TestMethod()> _
    Public Sub TestGetPicturesAsAtomItemCount()
        Dim reader As XmlReader = XmlReader.Create(BuildUri(ServiceUris.GetPicturesAsRss).ToString)
        Dim feed As SyndicationFeed = SyndicationFeed.Load(reader)
        Dim expected As Integer = PictureServiceSettings.DefaultFeedItemsCount
        Dim actual As Integer = feed.Items.Count()
        Assert.AreEqual(Of Integer)(expected, actual)
    End Sub

    Private Function BuildUri(ByVal uri As String) As Uri
        Dim baseAddress As New Uri(PictureServiceHostHelper.TestAddress)

        Return (New Uri(baseAddress, New Uri(uri, UriKind.Relative)))
    End Function

    Private Function ReadFeed(ByVal formatter As SyndicationFeedFormatter) As String
        Dim feed As String = Nothing

        Try
            Using stream As New MemoryStream()
                Dim writer As XmlWriter = XmlWriter.Create(stream)
                formatter.WriteTo(writer)
                Dim sr As New StreamReader(stream)
                feed = sr.ReadToEnd()
                sr.Dispose()
            End Using
        Catch

        End Try

        Return feed
    End Function
End Class

Friend Class PictureServiceHostHelper
	Friend Const TestAddress As String = "http://localhost:12345"
	Friend Shared Host As WebServiceHost = Nothing

	Friend Shared Sub Start()
		Dim service As New PictureService()
		Dim binding As New WebHttpBinding()

		Host = New WebServiceHost(service, New Uri(PictureServiceHostHelper.TestAddress))
		Host.AddServiceEndpoint(GetType(IPictureService), binding, "")
		Host.Open()
	End Sub

	Friend Shared Sub [Stop]()
		If Host.State <> CommunicationState.Closed Then
			Host.Close()
		End If
	End Sub
End Class
